package com.elephant.core.utils;

import com.elephant.core.socket.socketforxml.MessageBean;
import com.thoughtworks.xstream.XStream;

/**
 * @author hai
 * @description
 * @date 16:01 2018/8/31
 */
public class MessageXmlUtil {

    public static String toXml(Object object){

        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        return xstream.toXML(object);
    }


    public static Object toBean(Class<?> clazz, String xml) {
        Object xmlObject;
        XStream xstream = new XStream();
        xstream.processAnnotations(clazz);
        xstream.autodetectAnnotations(true);
        xmlObject= xstream.fromXML(xml);
        return xmlObject;
    }


    public static void main(String[] args) {

        MessageBean msg = new MessageBean();
        msg.setName("EVENT");
        msg.setNo("10004");
        msg.setType("RSP");

        msg.setContent("NULL");
        msg.setEventNo("3");
        msg.setStuNo("001003");
        msg.setEventName("学生吃饭了");
        msg.setMobilePhone("1383838438");


        String s = toXml(msg);
        System.out.println(s);


        System.out.println(toBean(MessageBean.class,s));
    }
}
